use entity::{booking, booking_status, user::custom};
use sea_orm_migration::{prelude::*, sea_orm::Schema};

pub struct Migration;

impl MigrationName for Migration {
    fn name(&self) -> &str {
        "m20220520_124906_create_booking"
    }
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let schema = Schema::new(manager.get_database_backend());

        let mut table = schema.create_table_from_entity(booking::Entity);
        table
            .if_not_exists()
            .foreign_key(
                ForeignKey::create()
                    .from_col(booking::Column::CustomId)
                    .to(custom::Entity, custom::Column::Id)
                    .on_update(ForeignKeyAction::Cascade)
                    .on_delete(ForeignKeyAction::Restrict),
            )
            .foreign_key(
                ForeignKey::create()
                    .from_col(booking::Column::BookDayId)
                    .to(booking_status::Entity, booking_status::Column::Id)
                    .on_update(ForeignKeyAction::Cascade)
                    .on_delete(ForeignKeyAction::Restrict),
            );

        manager.create_table(table).await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(booking::Entity).to_owned())
            .await
    }
}
